<?php

namespace App\Kuafu\Shop\Controller\Pos;

use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Shop\Enum\ShopCodeEnum;
use App\Kuafu\Shop\Model\CameraModel;
use App\Kuafu\Shop\Service\DeviceService;
use App\Kuafu\Shop\Validate\DeviceValidate;
use App\Kuafu\Shop\Validate\ShopValidate;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class DeviceController extends BaseController
{
    /**
     * 秒秒测设备绑定
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BaseException
     */
    public function bindMMC(Request $request)
    {
        $params             = DeviceValidate::bindMMCValidate(
            $request->merge(["shopId" => $request->headers->get("shopId")])->all()
        );
        $params->deviceType = CameraModel::DEVICE_TYPE_MMC;

        $result = DeviceService::bind($params);

        return $this->success([
            'id' => $result->id
        ]);
    }


    /**
     * 秒秒测设备解绑
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\CustomValidateException
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function rmBindMMC(Request $request)
    {
        $params = DeviceValidate::rmBindValidate(
            $request->merge([
                "shopId" => $request->headers->get("shopId")
            ])->all()
        );
        DeviceService::rmBind($params);
        return $this->success();
    }


    /**
     * POS 温度设备列表
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function listMMC(Request $request)
    {
        return $this->success(DeviceService::deviceMMCList($this->shopId));
    }


    /**
     * 接收温度秒秒测数据
     * @param Request $request
     * @return JsonResponse
     * @throws \App\Kuafu\Shop\Exceptions\ShopException
     */
    public function temperatureData(Request $request): JsonResponse
    {
        $data  = $request->post('data');
        $valid = ShopValidate::temperatureData($request);
        if ($valid) {
            return $this->fail(ShopCodeEnum::ERR_BANS_HAS_SERIAL_NUMBER, $valid);
        }

        $shopId = $request->header("shopId");
        DeviceService::addTemperatureData($shopId, $data);
        return $this->success();
    }

    /**
     * 查看温度秒秒测数据
     * @param Request $request
     * @return JsonResponse
     */
    public function getTemperatureData(Request $request)
    {
        $valid = ShopValidate::getTemperatureData($request);
        if ($valid) {
            return $this->fail(ShopCodeEnum::ERR_BANS_HAS_SERIAL_NUMBER, $valid);
        }
        $shopId = $request->header("shopId");
        $result = DeviceService::getTemperatureData($shopId, $request->all());
        return $this->success($result);
    }
}
